package per.mapt.spring.boot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import per.mapt.spring.boot.UserValidateFailedException;
import per.mapt.spring.boot.entity.User;
import per.mapt.spring.boot.mapper.UserMapper;

import java.util.Optional;

@Component
public class HelloMybatisImpl implements HelloMybatis {
    @Autowired
    private UserMapper mapper;

    @Override
    public void validateUser(String code, int id) {
       User user = Optional.ofNullable(mapper.selectUser(id)).orElseThrow(UserValidateFailedException::new);
       if(!Optional.ofNullable(code).orElseThrow(UserValidateFailedException::new).equals(user.getCode())){
           throw new UserValidateFailedException();
       }
    }
}
